package com.yf.exam.modules.trainingarchives.headteacherlog.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Preconditions;
import com.yf.exam.common.entity.Result;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.trainingarchives.headteacherlog.dto.req.HeadTeacherLogReqDTO;
import com.yf.exam.modules.trainingarchives.headteacherlog.dto.resp.HeadTeacherLogRespDTO;
import com.yf.exam.modules.trainingarchives.headteacherlog.service.IHeadTeacherLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 班主任工作日志表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Api(tags={"班主任工作日志"})
@Slf4j
@RestController
@RequestMapping("/head-teacher-log")
public class HeadTeacherLogController {

    @Resource
    private IHeadTeacherLogService iHeadTeacherLogService;

    /**
     * 新增班主任工作日志
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增班主任工作日志")
    public Result<Boolean> add(@RequestBody HeadTeacherLogReqDTO reqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("HeadTeacherLogController.add.reqDTO:{}", JSON.toJSONString(reqDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getTrainingClassInfoId()), "培训班id不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getDate()), "日期不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getTime()), "时间不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getWeather()), "天气不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(reqDTO.getLogContent()), "日志内容不能为空!");
            return Result.ok(iHeadTeacherLogService.add(reqDTO));
        } catch (Exception e) {
            log.error("HeadTeacherLogController.add.error:{}", e.getMessage(), e);
            return Result.faild("新增班主任工作日志失败!");
        }
    }

    /**
     * 删除班主任工作日志
     * @param baseIdReqDTO
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除班主任工作日志")
    public Result<Boolean> delete(@RequestBody BaseIdReqDTO baseIdReqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("HeadTeacherLogController.delete.elProjectReqDTO:{}", JSON.toJSONString(baseIdReqDTO));
            }
            Preconditions.checkNotNull(baseIdReqDTO.getId(), "班主任工作日志id不能为空!");
            Boolean result = iHeadTeacherLogService.deleteById(baseIdReqDTO.getId());
            return Result.ok(result);
        } catch (Exception e) {
            log.error("HeadTeacherLogController.delete.error:{}", e.getMessage(), e);
            return Result.faild("删除班主任工作日志失败!");
        }

    }

    /**
     * 更新项目
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新班主任工作日志")
    public Result<Boolean> update(@RequestBody HeadTeacherLogReqDTO reqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("HeadTeacherLogController.update.reqDTO:{}", JSON.toJSONString(reqDTO));
            }
            Preconditions.checkNotNull(reqDTO.getId(), "班主任工作日志id不能为空!");
            Boolean result = iHeadTeacherLogService.updateHeadTeacherLog(reqDTO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("HeadTeacherLogController.update.error:{}", e.getMessage(), e);
            return Result.faild("更新班主任工作日志失败");
        }
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public Result<IPage<HeadTeacherLogRespDTO>> paging(@RequestBody PagingReqDTO<HeadTeacherLogReqDTO> reqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("HeadTeacherLogController.paging.reqDTO:{}", JSON.toJSONString(reqDTO));
            }
            //分页查询并转换
            IPage<HeadTeacherLogRespDTO> page = iHeadTeacherLogService.paging(reqDTO);
            return Result.ok(page);
        } catch (Exception e) {
            log.error("HeadTeacherLogController.paging.error:{}", e.getMessage(), e);
            return Result.faild("查询失败");
        }
    }

    /**
     * 班主任工作日志详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "获取班主任工作日志详情")
    public Result<HeadTeacherLogRespDTO> detail(@RequestBody BaseIdReqDTO baseIdReqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("HeadTeacherLogController.detail.baseIdReqDTO:{}", JSON.toJSONString(baseIdReqDTO));
            }
            Preconditions.checkArgument(!(baseIdReqDTO.getId() == null), "班主任工作日志ID不能为空!");
            return Result.ok(iHeadTeacherLogService.getDetail(baseIdReqDTO));
        } catch (Exception e) {
            log.error("HeadTeacherLogController.detail.error:{}", e.getMessage(), e);
            return Result.faild("查询班主任工作日志详情失败!");
        }
    }

}
